﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

namespace Bjsubway
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            lblWeather.Text = "北京 \n晴 1-4C";
            //中间 内容播放区
            DefaultForm();
        }
        private void DefaultForm()
        {
            CreateFormInstance(typeof(DefaultForm).Name,null);
            return;

        }
        private void btnSubwayTransfer_Click(object sender, EventArgs e)
        {
            CreateFormInstance(typeof(SubwayTransferForm).Name,sender);
          
            return;
        
        }
        /// <summary>
        /// 切换form实例 ,设置颜色
        /// </summary>
        /// <param name="formName">form的类名</param>
        private void CreateFormInstance(string formName,object sender)
        {
            //设置颜色
            SetBtnClickColor(sender);
            //遍历主窗口上的所有子控件
            Control[] curControls = this.sContainerMiddle.Panel1.Controls.Find(formName, true);
            if ((curControls != null) && (curControls.Length > 0) && (curControls[0].GetType().Name == formName))
            {
                //显示当前tab;
                Form tForm = (Form)curControls[0];
                tForm.Activate();
                return;
            }
            else
            {
                //清除所有原来的form
                this.sContainerMiddle.Panel1.Controls.Clear();
                //如果不存在则用反射创建form窗体实例。
                Assembly asm = Assembly.Load("Bjsubway");//程序集名
                object frmObj = null;
                try
                {
                    frmObj = Activator.CreateInstance(Type.GetType("Bjsubway." + formName), null);

                    Form childForm = (Form)frmObj;
                    childForm.TopLevel = false;
                    childForm.FormBorderStyle = FormBorderStyle.None;
                    childForm.Dock = DockStyle.Fill;
                    childForm.Tag = childForm.Name;
                    childForm.Visible = true;
                    this.sContainerMiddle.Panel1.Controls.Add(childForm);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return;
                }
            }
        }

        private void SetBtnClickColor(object sender)
        {
            foreach (Control ctrl in sContainerMiddle.Panel2.Controls)
            {
                if (ctrl is Button)
                {
                    ctrl.ForeColor = Color.Black;
                }
            }
            if (sender == null) return;
            ((Button)sender).ForeColor = Color.Red;
        }
        private void btnBusTransfer_Click(object sender, EventArgs e)
        {
            CreateFormInstance(typeof(BusTransferForm).Name,sender);
            return;
        }

        private void btnSubmayTime_Click(object sender, EventArgs e)
        {
            CreateFormInstance(typeof(SubwayTimeForm).Name, sender);
            return;
        }

        private void btnCircumstance_Click(object sender, EventArgs e)
        {
            CreateFormInstance(typeof(CirsumestanceForm).Name, sender);
            return;
        }

        private void btnNews_Click(object sender, EventArgs e)
        {
            CreateFormInstance(typeof(NewsNoteForm).Name, sender);
            return;
        }

        private void btnStation_Click(object sender, EventArgs e)
        {
            CreateFormInstance(typeof(StationInfoForm).Name, sender);
            return;
        }

        private void btnPassener_Click(object sender, EventArgs e)
        {
            CreateFormInstance(typeof(PassenerNoticeForm).Name, sender);
            return;
        }

        private void btnSafe_Click(object sender, EventArgs e)
        {
            CreateFormInstance(typeof(SafeServiceForm).Name, sender);
            return;
        }

        private void btnConvenientService_Click(object sender, EventArgs e)
        {
            CreateFormInstance(typeof(ConvenientServiceForm).Name, sender);
            return;
        }

        private void btnWeather_Click(object sender, EventArgs e)
        {
            CreateFormInstance(typeof(WeatherPredictForm).Name, sender);
            return;
        }
    }
}
